import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_presenter.dart';

import 'base_mvp_view.dart';

abstract class BaseWidget extends StatefulWidget {
  BaseWidgetState baseWidgetState;

  @override
  State<StatefulWidget> createState() {
    baseWidgetState = getState();
    return baseWidgetState;
  }

  //通过子类获取
  BaseWidgetState getState();
}

abstract class BaseWidgetState<P extends BasePresenter, T extends BaseWidget>
    extends State<T> with BaseMvpView {
  P mPresenter;
  String appBarText = "";
  List<Widget> actions = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initBaseCommon(this, context);
    mPresenter = initPresenter();
    if (mPresenter != null) mPresenter.attachView(this);
    initData();
  }

  void initData();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:  AppBar(
       title: appBarText == "" ? null : Text(appBarText),
        actions: actions,
        elevation: 0,
      ),
      body: getBaseView(context),
      resizeToAvoidBottomPadding: false, //输入框抵住键盘
    );
  }

  void setAppBarText(String title) {
    setState(() {
      appBarText = title;
    });
  }

  void setAppbarActions(List<Widget> action){
    setState(() {
      actions = action;
    });
  }

  @override
  String className() {
    // TODO: implement className
    return null;
  }

  @override
  void dismissLoading() {
    setLoadingVisible(false);
  }

  @override
  void showLoading(String msg) {
    setLoadingVisible(true, msg);
  }
}
